home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3354 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  94 lines

  1. Newsgroups: comp.lang.c
  2. Path: FreeNet.Carleton.CA!ab598
  3. From: ab598@FreeNet.Carleton.CA (David Biggar)
  4. Subject: Re: array of struct pointer switching difficulties
  5. Message-ID: <DLvLHq.K2x@freenet.carleton.ca>
  6. Sender: ab598@freenet3.carleton.ca (David Biggar)
  7. Reply-To: ab598@FreeNet.Carleton.CA (David Biggar)
  8. Organization: The National Capital FreeNet
  9. References:  <96012622065929517@tglbbs.com>
  10. Date: Sun, 28 Jan 1996 05:20:13 GMT
  11.  
  12.  
  13. Charles Herold (charles.herold@tglbbs.com) writes:
  14. > I posted a question regarding reversing an array of pointers.  Two
  15. > people posted similar answers, but neither works, so perhaps there is a
  16. > more fundamental problem with my code.  Therefore I have put together a
  17. > full little program that will get the files of a directory, reverse the
  18. > order they're in, and print them out, so people can see what I've done,
  19. > and hopefully tell me what I'm doing wrong.  In SortFiles, a version of
  20. > which I posted last time, I have both the reverse sort I use and the one
  21. > suggested to me.  I apologize for posting such a long piece of code, but
  22. > this seems to be the minimum necessary to show you what the problem is.
  23. >
  24. >   [some code deleted]
  25. >
  26. > struct find_t *files;
  27. >    more code deleted]
  28. >
  29. > void SortFiles( struct dir_window *dwindow, int (*compare)(const
  30. >     void *elem1, const void *elem2))
  31. > {
  32. >     int i, h;
  33. >     struct find_t temp, **files = dwindow->files;
  34. >     /* There's a qsort here I've removed as extraneous */
  35. >     if( reverse_sort ) {    /* make it all backwards */
  36. > #if defined SHOULD_WORK   /* told this should work, but doesn't */
  37. >          struct find_t *temp;
  38. >         for (i=0; i < dwindow->numberfiles / 2; i++) {
  39. >               temp = files[i];
  40. >               files[i] = files[dwindow->numberfiles - i - 1];
  41. >              files[dwindow->numberfiles - i - 1] = temp;
  42. >          }
  43. > #else
  44. >         for( i = 0; i < dwindow->numberfiles / 2; i++ ) {
  45. >               /* now reverse the entire array */
  46. >               temp = (*files)[i];
  47. >              (*files)[i] = (*files)[dwindow->numberfiles - i - 1];
  48. >               (*files)[dwindow->numberfiles - i - 1] = temp;
  49. >         }
  50. > #endif
  51. >  ................[code deleted]
  52. >
  53. > main( int argc, char *argv[] )
  54. > {
  55. >     if( !(window.numberfiles = GetDirectory( &window, &files) ) ) {
  56. >          fprintf( stderr, "no files found" );
  57. >          return 1;
  58. >     }
  59. > }
  60. __________________________________________________________________
  61.  
  62.  
  63. Assuming your solution is the "#else" part, I believe that's 
  64. correct.
  65.  
  66. The "SHOULD WORK" part is not correct. 
  67.  
  68. Note that in SortFiles(), the local variable "files" is assigned 
  69. a value of 'ptr to ptr to struct find_t'; the assigned value 
  70. is the same value as '&files', where 'files' is the global 'files'
  71. whose address was passed to GetDirectory() in main(). In the 
  72. following, I refer to the global 'files' as 'Files'.
  73.  
  74. So, 'files[i]' in SortFiles() -> *((&Files) + i)
  75.  
  76. But, the only valid value of 'ptr to struct find_t' is for i = 0;
  77. for i > 0, values of '*((&Files) + i)'are garbage.
  78.  
  79. _______________________________________________________________
  80.  
  81. On the other hand, I believe that you have used  an 
  82. unnecessary level of indirection; i.e., from main(), you should
  83. call GetDirectory() with 'files' rather than '&files'. You
  84. would have to make corresponding changes in several of the other
  85. functions but I believe the code would be more readable. The
  86. change should result in the 'SHOULD WORK' code being correct :-)
  87.  
  88. _______________________________________________________________
  89.  
  90.  
  91. Dave
  92.